package com.a3dweijing.wetouch2.fragment;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.a3dweijing.wetouch2.R;
import com.a3dweijing.wetouch2.adapter.BannerImageLoader;
import com.a3dweijing.wetouch2.adapter.ThreeDTVViewpagerAdapter;
import com.a3dweijing.wetouch2.bean.TVBannerMsg;
import com.a3dweijing.wetouch2.callback.OnClick;
import com.a3dweijing.wetouch2.datainterface.Getdataimpl;
import com.google.gson.Gson;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.Transformer;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Yxd on 2017/10/31.
 */

public class ThreeDTVFragment extends Fragment implements OnClick {

    private Handler handler;
    private Banner banner;
    private TabLayout tabLayout;
    private ViewPager viewpager;
    private List<String> tabTitle;
    private List<Fragment> fragments;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.fragment_threed_tv,container,false);
        banner= (Banner) view.findViewById(R.id.tv_banner);

        dataBuilding();

        tabLayout= (TabLayout) view.findViewById(R.id.tv_tab);
        viewpager= (ViewPager) view.findViewById(R.id.tv_viewpager);
        viewpager.setAdapter(new ThreeDTVViewpagerAdapter(getChildFragmentManager(),fragments,tabTitle));
        tabLayout.setupWithViewPager(viewpager);
        LinearLayout tabLayoutChildAt= (LinearLayout) tabLayout.getChildAt(0);
        tabLayoutChildAt.setShowDividers(LinearLayout.SHOW_DIVIDER_MIDDLE);
        tabLayoutChildAt.setDividerDrawable(getContext().getResources().getDrawable(R.drawable.tablayout_tab_divider));

        handler=new Handler(){
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                switch (msg.what)
                {
                    case 100:
                        TVBannerMsg bannerMsg=new Gson().fromJson(msg.obj.toString(),TVBannerMsg.class);
                        if (bannerMsg.isFlag())
                        {
                            banner.setVisibility(View.VISIBLE);
                            bannerInit(bannerMsg.getList());
                        }else {
                            banner.setVisibility(View.GONE);
                        }
                       break;
                    case 404:
//                        Toast.makeText(getContext(),"网络繁忙，请稍后再试，",Toast.LENGTH_SHORT).show();
                        banner.setVisibility(View.GONE);
                        break;
                }
            }
        };
        new GetTVbannerThread().start();
        return view;
    }

    public static ThreeDTVFragment newinstance()
    {
        Bundle bundle=new Bundle();
        bundle.putString("name","影视");
        ThreeDTVFragment threeDTVFragment =new ThreeDTVFragment();
        threeDTVFragment.setArguments(bundle);
        return threeDTVFragment;
    }

    @Override
    public void onclick(int position,Object o) {

    }

    private class GetTVbannerThread extends Thread
    {

        @Override
        public void run() {
            super.run();
            String resulet=new Getdataimpl().remotGetTVBanner();
            if (resulet!=null && resulet.length()>7)
            {
                handler.sendMessage(handler.obtainMessage(100,resulet));
            }else {
                handler.sendMessage(handler.obtainMessage(404,"服务器繁忙，请稍后再试。"));
            }
        }
    }
    private void dataBuilding(){


        tabTitle=new ArrayList<>();
        tabTitle.add("最新");
        tabTitle.add("最热");

        fragments=new ArrayList<>();
        fragments.add(TVVideoFragment.newinstance());
        fragments.add(TVVideoHotFragment.newinstance());

    }
    private void bannerInit(List mListImage){
        banner.setVisibility(View.VISIBLE);
        banner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR);
        //设置图片加载器
        banner.setImageLoader(new BannerImageLoader());
        //实例化图片集合
        //设置Banner图片集合
        banner.setImages(mListImage);
        //设置Banner动画效果
        banner.setBannerAnimation(Transformer.DepthPage);
        //实例化Title集合

        //设置Banner标题集合（当banner样式有显示title时）
//        banner.setBannerTitles(mListTitle);
        //设置轮播时间
        banner.setDelayTime(1000);
        //设置指示器位置（当banner模式中有指示器时）
        banner.setIndicatorGravity(BannerConfig.RIGHT);
        //Banner设置方法全部调用完毕时最后调用
        banner.start();
    }
}
